﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TS.Commons;
using TS.Entities;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Globalization;

namespace TS.DataAccess
{
    public class GroupDataAccess
    {
        /// <summary>
        /// Parse To Entity
        /// </summary>
        /// <param name="iDataReader"></param>
        /// <returns></returns>
        public static Group ParseToEntity(IDataRecord dataRecord)
        {
            return new Group
            {
                GroupId = dataRecord["GroupId"].ToStringOrDefault().ToNumeric(),
                Name = dataRecord["Name"].ToStringOrDefault(),
                Description = dataRecord["Description"].ToStringOrDefault()
            };
        }

        /// <summary>
        /// Get All Group info
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Collection<Group> GetAllGroup()
        {
            Collection<SqlParameter> collection = new Collection<SqlParameter>
                                                      {
                                                      };
            int totalRecord = 0;
            using (IDataReader reader = SqlHelper.ExecuteReader(UIUser.SelectGroupQuery, collection))
            {
                var result = new Collection<Group>();
                while (reader.Read())
                {
                    Group grp = ParseToEntity(reader);
                    grp.Description = Utility.WriteString(grp.Description, 20);
                    result.Add(grp);
                }
                if (reader.NextResult())
                {
                    if (reader.Read())
                    {
                        totalRecord = reader["TotalRowCount"].ToNumeric();
                    }
                }
                return result;
            }

        }

        public Collection<Group> ReadGroupPaged(int pageSize, int pageIndex, string strFilter, string strOrderBy, out int totalRecord)
        {
            var parameters = new Collection<SqlParameter>
                                                      {
                                                          new SqlParameter("@PageSize",pageSize),
                                                          new SqlParameter("@PageIndex",pageIndex),
                                                          new SqlParameter("@StringFilter",strFilter),
                                                          new SqlParameter("@OrderBy",strOrderBy)
                                                      };
            totalRecord = 0;

            //get result
            using (IDataReader reader = SqlHelper.ExecuteReader(UIUser.GroupSelectPagedQuery, parameters))
            {
                var result = new Collection<Group>();
                while (reader.Read())
                {
                    Group Group = ParseToEntity(reader);
                    result.Add(Group);
                }
                if (reader.NextResult())
                {
                    if (reader.Read())
                    {
                        totalRecord = reader["TotalRowCount"].ToNumeric();
                    }
                }
                return result;
            }
        }


    }
}
